$(function() {
	$("#assetsSorts").select2({
		placeholder : "请选择类别"
	});
	$("#assetsStatus").select2({
		placeholder : "请选择状态"
	});
	companyList();
});

function updateAssets(assetsId) {
	var url = '/IMS/assetsInformation/updateAssets.ht';
	var pcid = $("#pcid").val();
	var flag = "0";
	if (!assetsId) {
		return false;
	} else {
		url = url + "?assetsId=" + assetsId + "&flag=" + flag + "&pcid=" + pcid;
		$.go(url, {
			dataType : 'html',
			beforeSend : function() {
				showProccessBar();
			},
			error : function() {
				hideProccessBar();
			},
			success : function(data, textStatus) {
				replaceDocument(url, data);
			}
		});
	}
}

function closeDialogLock(obj) {
	closeDialog(obj);
	window.location.reload();
}

function exportExcel() {// 导出模板
	if (result.list != null && result.list.length > 0) {
		showDialog('ks-pop-box3');
	} else {
		_alert("没有数据，无法导出");
		return;
	}
}

function exportExcels() {
	var pcid = null;
	var cid = null;
	var pname = null;
	var cname = null;
	if ($("#flag").val() == 3) {
		pcid = $("#cpSelect").val();
		cid = $("#cids").val();
		cname = $("#cname").text();
		pname = $("#pname").text();
	} else {
		pcid = $("#cpSelect2").val();
		cid = $("#cid").val();
		pname = $("#select2-cpSelect2-container").text();
		cname = $("#select2-cid-container").text();
	}
	var assetsSorts = $("#assetsSorts").val();
	var assetsStatus = $("#assetsStatus").val();
	if (pcid == null || pcid == '' || pcid == "请选择公司") {
		_alert("请选择公司");
		return;
	}
	if (cid == null || cid == '' || cid == '请选择小区') {
		cid = '';
	}
	if (cname == "请选择小区") {
		cname = '';
	}
	var controlUrl = "/IMS/assetsInformation/exportExcel.ht";
	var url = controlUrl + "?pcid=" + pcid + "&cid=" + cid + "&assetsSorts=" + assetsSorts + "&assetsStatus=" + assetsStatus + "&cname=" + cname + "&pname=" + pname;
	$.ajax({
		url : '/IMS/securityProtect/check.ht',
		data : {
			url : controlUrl
		},
		beforeSend : function() {
			showProccessBar();
		},
		error : function() {
			hideProccessBar();
		},
		success : function() {
			hideProccessBar();
			window.open(url);
		}
	});
}

function change() {
	$("#select2-cid-container").attr("title", null);
	$("#select2-cid-container").text('');
	$("#cid").children().remove();
	initVillage();
}

function companyList() {
	var pcid = $("#pcid").val();
	var url = "/IMS/select/getCompanyListByPcid.ht?pcid=" + pcid;
	if (userType != '3') {
		// 先判断是否只有一条数据
		$.ajax({
			url : url,
			dataType : 'json',
			beforeSend : function() {
				showProccessBar();
			},
			error : function() {
				hideProccessBar();
			},
			success : function(data) {
				if (data != null && data.total == 1) {
					$("#companySelect").remove();
					var pcid = data.list[0].id;
					var pcName = data.list[0].name;
					var companyHtml = "<input type='hidden' id='cpSelect2' name='pcid' value='" + pcid + "' /><span>" + pcName + "</span>";
					$("#companyHtml").append(companyHtml);

					// 加载小区
					$.ajax({
						url : '/IMS/select/getCommunityListByCid.ht',
						data : {
							pcid : pcid
						},
						dataType : 'json',
						success : function(data) {
							if (data != null && data.total == 1) {
								$("#cid").remove();
								var cid = data.list[0].id;
								var cName = data.list[0].name;
								var cNameHtml = "<input type='hidden' id='cid' name='cid' value='" + cid + "' /><span>" + cName + "</span>";
								$("#controlIds").append(cNameHtml);
							} else {
								$("#cid").select2({
									placeholder : "请选择小区"
								})
								initVillage();
							}
							hideProccessBar();
						},
						error : function() {
							$("#cid").select2({
								placeholder : "请选择小区"
							})
							initVillage();
							hideProccessBar();
						}
					});
				} else {
					$("#cid").select2({
						placeholder : "请选择小区"
					})
					initCompanyWidget("cpSelect2", url, true);
					hideProccessBar();
				}
			},
			error : function() {
				$("#cid").select2({
					placeholder : "请选择小区"
				})
				initCompanyWidget("cpSelect2", url);
			}
		});
	} else {
		$("#cid").select2({
			placeholder : "请选择小区"
		})
		initCompanyWidget("cpSelect2", url);
	}
}

function initVillage() {
	$("#controlIds").attr("class", "control-css3select item-box-p1 css3select-icon-hide");
	$("#cid").select2({
		placeholder : "请选择小区",
		language : "zh-CN",
		allowClear : true,
		ajax : {
			url : '/IMS/select/getCommunityListByCid.ht',
			dataType : 'json',
			delay : 250,
			data : function(param) {
				return {
					search : param.term,// 输入的内容，会发给服务器进行查找
					// 以下两行可以在数据量大的时候用到（当下拉框往下拉的时候会加载下一页），数据量不大的时候，可以直接注释
					page : param.page || 1, // 当前页
					pcid : function() {
						return $("#cpSelect2").val();
					},
					showAllItem : true
				};
			},
			processResults : function(data, param) {
				param.page = data.pageNum;
				return {
					results : data.list,
					pagination : {
						more : data.hasNextPage
					}
				};
			}
		},
		escapeMarkup : function(markup) {
			return markup;
		},
		templateResult : formatRepo, // 显示查询结果
		templateSelection : function(obj) {
			$("#cid").val(obj.id);
			return obj.name || obj.text;
		} // 显示选中的对象
	});
}

function delectAssets(assetsId) {
	if (!assetsId) {
		return false;
	} else {
		var msg = '是否删除资产';
		_confirm("<span style='color:#FE6600'>" + msg + "</span>", function() {
			$.ajax({
				type : "POST",
				url : '/IMS/assetsInformation/delete.ht',
				data : {
					assetsId : assetsId
				},
				beforeSend : function() {
					showProccessBar();
				},
				error : function() {
					hideProccessBar();
				},
				success : function(data, status) {
					if (data.msgCode == 1) {
						_alert(data.msg);
						hideProccessBar();
					} else if (data.msgCode == 0) {
						_alert(data.msg, true);
						url = '/IMS/assetsInformation/assetsList.ht';
						var pcid = $("#pcid").val();
						url = url + "?pcid=" + pcid;
						$.go(url, {
							dataType : 'html',
							beforeSend : function() {
								showProccessBar();
							},
							error : function() {
								hideProccessBar();
							},
							success : function(data, textStatus) {
								replaceDocument(url, data);
							}
						});
					}
				},
			});
		});
	}
}

function view(assetsId) {
	var url = '/IMS/assetsInformation/seeassets.ht';
	if (!assetsId) {
		return false;
	} else {
		url = url + "?assetsId=" + assetsId;
		$.go(url, {
			dataType : 'html',
			beforeSend : function() {
				showProccessBar();
			},
			error : function() {
				hideProccessBar();
			},
			success : function(data, textStatus) {
				replaceDocument(url, data);
			}
		});
	}
}
function returnSuperior() {
	var pcid = $("#pcid").val();
	var url = "/IMS/estate/view.ht?checkUserId=" + pcid;
	$.go(url, {
		dataType : 'html',
		beforeSend : function() {
			showProccessBar();
		},
		error : function() {
			hideProccessBar();
		},
		success : function(data, textStatus) {
			replaceDocument(url, data);
		}
	});
}

function print(formId, msg) {
	if (!formId || typeof _url === "String") {
		return false;
	}
	if (!printUrl || typeof printUrl === "String") {
		return false;
	}
	if (!$("input[name='ids']")) {
		_alert("没有可选项");
		return false;
	}
	if ($("#" + formId + " input[name='ids']:checked").length <= 0) {
		_alert("请选择您要打印的记录");
		return false;
	} else if ($("#" + formId + " input[name='ids']:checked").length > 20) {
		_alert("请选择少于20条资产信息");
		return false;
	}

	msg = msg ? msg : '是否打印所选记录';
	_confirm("<span style='color:#FE6600'>" + msg + "</span>", function() {
		var form = document.getElementById(formId);
		var url = printUrl;
		var idObjs = $("#" + formId + " input[name='ids']:checked");
		for(var i in idObjs){
			var idObj = idObjs[i];
			if(idObj && idObj.value){
				url += '&ids=' + idObj.value; 
			}
		}
		$.submit($("#" + formId), {
			url : printUrl,
			dataType : 'html',
			beforeSend : showProccessBar,
			complete : hideProccessBar,
			success : function(data, textStatus) {
				replaceDocument(url, data);
			}
		});
	});
}

function searchSubmit() {
	/*
	 * var search = $("#search").val(); if (Is_Null(search)) { _alert("请输入查询条件后再进行搜索"); return; }
	 */
	$.submit($('#frm'), {
		dataType : 'html',
		beforeSend : showProccessBar,
		complete : hideProccessBar,
		success : function(data, textStatus) {
			var url = '/IMS/assetsInformation/assetsList.ht?pcid=' + _PCID;
			// var url = $('#frm').attr('action');
			replaceDocument(url, data);
		}
	});
}